package com.longtop.sp.mapdao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.util.HttpSessionMutexListener;

public class MapDao {
	private static Log log = LogFactory.getLog(MapDao.class);
	private Insert insert;
	private Update update;
	private Delete delete;
	private Load load;
	private JdbcTemplate jt;

	/**
	 * 鏍规嵁琛ㄥ悕鍜屽鍚堜富閿煡璇�
	 * 
	 * @param tableName
	 *            琛ㄥ悕
	 * @param ids
	 *            涓婚敭闆嗗悎
	 * @return
	 * @throws Exception
	 */
	public MapVO load(String tableName, Map ids) throws Exception {
		MapVO vo = generateMapVO(tableName, ids);
		load.setMapVO(vo);
		return load.load(ids);
	}

	/**
	 * 鏍规嵁琛ㄥ悕銆佷富閿悕鍜屼富閿�兼煡璇�
	 * 
	 * @param tableName
	 *            琛ㄥ悕
	 * @param pkName
	 *            涓婚敭鍚�
	 * @param pkValue
	 *            涓婚敭鍊�
	 * @return
	 * @throws Exception
	 */
	public MapVO load(String tableName, String pkName, Object pkValue)
			throws Exception {
		Map ids = new HashMap();
		ids.put(pkName, pkValue);
		return load(tableName, ids);
	}

	/**
	 * 鏍规嵁琛ㄥ悕鍜屽敮涓�涓婚敭鏌ヨ
	 * 
	 * @param tableName
	 *            琛ㄥ悕
	 * @param pkValue
	 *            涓婚敭鍊�
	 * @return
	 * @throws Exception
	 */
	public MapVO load(String tableName, Object pkValue) throws Exception {
		MapVO vo = generateMapVO(tableName, null);
		load.setMapVO(vo);
		return load.loadBySinglePk(pkValue);
	}

	/**
	 * 鏂板涓�鏉¤褰�
	 * 
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public int add(MapVO vo) throws Exception {
		insert.setMapVO(vo);
		try {
			return insert.execute();
		} catch (Exception e) {
			log.error("鍚戣〃" + vo.getTableName() + "鏂板璁板綍澶辫触" + e.getMessage());
			throw e;
		}
	}

	/**
	 * 鏇存柊涓�鏉¤褰�
	 * 
	 * @param vo
	 *            vo涓寘鎷繀椤荤殑琛ㄥ悕銆佷富閿俊鎭拰闇�瑕佹洿鏂扮殑瀛楁淇℃伅
	 * @return
	 * @throws Exception
	 */
	public int update(MapVO vo) throws Exception {
		update.setMapVO(vo);
		try {
			return update.execute();
		} catch (Exception e) {
			log.error("鍚戣〃" + vo.getTableName() + "淇敼璁板綍澶辫触" + e.getMessage());
			throw e;
		}
	}

	public int delete(MapVO vo) throws Exception {
		delete.setMapVO(vo);
		try {
			return delete.execute();
		} catch (Exception e) {
			log.error("鍚戣〃" + vo.getTableName() + "淇敼璁板綍澶辫触" + e.getMessage());
			throw e;
		}
	}

	public int delete(String tableName, String id) throws Exception {
		return delete(tableName, "ID", id);
	}

	public int delete(String tableName, String primaryKeyName, String id)
			throws Exception {
		return delete(tableName, primaryKeyName + "=?", new Object[] { id });
	}

	public int delete(String tableName, String cause, Object[] args)
			throws Exception {
		String sql = "DELETE FROM " + tableName + " WHERE " + cause;
		return jt.update(sql, args);
	}

	/**
	 * 鏍规嵁琛ㄥ悕銆佹煡璇㈡潯浠跺拰鍙傛暟鏌ヨ
	 * 
	 * @param tableName
	 *            琛ㄥ悕
	 * @param cause
	 *            鏌ヨ鏉′欢(eg. "PROC_ID=?")
	 * @param args
	 *            鍙傛暟(eg. new String[] { procId })
	 * @return
	 * @throws Exception
	 */
	public List<MapVO> query(String tableName, String cause, Object[] args)
			throws Exception {
		String sql = "SELECT * FROM (" + tableName +") v";
		if (!StringUtils.isBlank(cause)) {
			sql += " WHERE " + cause;
		}

		List<Map<String, Object>> result = null;

		if (!StringUtils.isBlank(cause)) {
			result = jt.queryForList(sql, args);
		} else {
			result = jt.queryForList(sql);
		}
		if (result != null && !result.isEmpty()) {
			List<MapVO> ret = new ArrayList<MapVO>();
			for (Map<String, Object> map : result) {
				MapVO vo = generateMapVO(tableName, map);
				ret.add(vo);
			}
			return ret;
		}
		return null;
	}

	public List<MapVO> query(String tableName) throws Exception {
		return query(tableName, null, null);
	}

	public List<Map<String, Object>> queryList(String tableName, String cause,
			Object[] args) throws Exception {

		String sql = "SELECT * FROM " + tableName + " WHERE " + cause;
		List<Map<String, Object>> result = jt.queryForList(sql, args);
		return result;
	}

	public Map<String, Object> queryMapByPrimary(String tableName,
			String primaryKeyName, String primaryKeyValue) throws Exception {

		String sql = "SELECT * FROM " + tableName + " WHERE " + primaryKeyName
				+ "= '"+primaryKeyValue+"'" ;
		List<Map> rs = jt.queryForList(sql);
		List<Map<String, Object>> result = jt.queryForList(sql);
		if (result != null && !result.isEmpty()) {
			return result.get(0);
		}
		return null;
	}

	private MapVO generateMapVO(String tableName, Map<String, Object> map) {
		MapVO vo = new MapVO(tableName);
		vo.putAll(map);
		return vo;

	}

	public void setLoad(Load load) {
		this.load = load;
	}

	public void setDelete(Delete delete) {
		this.delete = delete;
	}

	public JdbcTemplate getJt() {
		return jt;
	}

	public void setInsert(Insert insert) {
		this.insert = insert;
	}

	public void setUpdate(Update update) {
		this.update = update;
	}

	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}
}